//获取应用实例
var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var LocalCart = require("../../LocalCart.js");

Page({
    data:{
        isFrom:"mine"
    },
    //请求数据
    Presenter:{
        init:function(that){
            var user = Global.getUserStorage();
            var data = { "action": "list","userId":user.id };
            MyRequest.MyGetWithToast("Address", that, that.Binder.init, data);
        }
    },
    //数据绑定
    Binder:{
        init:function(result,that){
            that.setData({
                address:result.data.data
            });
        }
    },
    toNewAddress:function(){
        var obj = "?";
        obj += "isNEW=0";
        wx.navigateTo({
      url: '/pages/newAddress/newAddress' + obj
    });
    },
    toUpadateAddress:function(event){
        var data = this.data.address[event.currentTarget.dataset.index]; 
        if(this.data.isFrom == "cart"){
            LocalCart.setAddressStorage(data);
            wx.navigateBack();
        }else{
            var obj = "?";
            obj += "isNEW=1";
            obj += "&id=";
            obj += data.id;
            obj += "&builderId=";
            obj += data.builderId;
            obj += "&phone=";
            obj += data.phone;
            obj += "&builderName=";
            obj += data.builderName;
            obj += "&builderId=";
            obj += data.builderId;
            obj += "&consignee=";
            obj += data.consignee;
            obj += "&detail=";
            obj += data.detail;
            wx.navigateTo({
                url: '/pages/newAddress/newAddress'+obj
            });
        }
    },
    onLoad:function(options){
        this.Presenter.init(this);
        if(options.isFrom){
            this.setData({
                isFrom:options.isFrom
            });
        }
    },
    onPullDownRefresh:function(){
        this.Presenter.init(this);
    }
})